package com.after.saas.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(SwaggerProperties swaggerProperties) {
        System.out.println("enabled="+swaggerProperties.getEnabled()+",description="+swaggerProperties.getDescription()+",basePackage="+swaggerProperties.getBasePackage());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerProperties.getEnabled())
                //apiInfo指定测试文档基本信息，这部分将在页面展示
                .apiInfo(apiInfo(swaggerProperties))
                .select()
                //apis() 控制哪些接口暴露给swagger，
                // RequestHandlerSelectors.any() 所有都暴露
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .version(swaggerProperties.getVersion())
                .license(swaggerProperties.getLicense())
                .licenseUrl(swaggerProperties.getLicenseUrl())
                .description(swaggerProperties.getDescription())
                .termsOfServiceUrl("")
                //联系人实体类
                .contact(buildContact(swaggerProperties.getContact())).build();
    }

    private Contact buildContact(com.after.saas.swagger.config.Contact contact) {
        return new Contact(contact.getName(), contact.getUrl(), contact.getEmail());
    }
}
